package admin

import (
	ct "context"
	"github.com/gin-gonic/gin"
	"golang.org/x/net/context"
	"net/http"
	"strconv"
	"strings"
	. "xm_shop/models"
	pbRbac "xm_shop/proto/rbacRole"
)

type RoleController struct{}

// 获取全部角色
func (ro RoleController) GetRole(ctx *gin.Context) {
	rbacClient := pbRbac.NewRbacRoleService("rbac", MicroClient)
	res, _ := rbacClient.RoleGet(context.Background(), &pbRbac.RoleGetRequest{})
	ctx.JSON(http.StatusOK, gin.H{
		"roleList": res.RoleResult,
	})

}

// 增加角色
func (ro RoleController) AddRole(ctx *gin.Context) {
	title := strings.Trim(ctx.PostForm("title"), " ")             // 去除空格
	description := strings.Trim(ctx.PostForm("description"), " ") // 去除空格
	if title == "" {
		ctx.String(http.StatusOK, "标题不能为空")
		return
	}
	// 添加记录
	rbacClient := pbRbac.NewRbacRoleService("rbac", MicroClient)
	res, _ := rbacClient.RoleAdd(context.Background(), &pbRbac.RoleAddRequest{
		Title:       title,
		Description: description,
	})
	if !res.Success {
		ctx.String(http.StatusOK, "添加角色失败")
	}
	ctx.String(http.StatusOK, "添加角色成功")
}

// 查一条
func (ro RoleController) GetOnlyRole(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		ctx.String(http.StatusOK, "类型转换出错, 数据不合法")
		return
	}
	rbacClient := pbRbac.NewRbacRoleService("rbac", MicroClient)
	res, _ := rbacClient.RoleGet(context.Background(), &pbRbac.RoleGetRequest{
		Id: int64(id),
	})
	ctx.JSON(http.StatusOK, gin.H{
		"result": res.RoleResult[0],
	})
}

// 编辑
func (ro RoleController) EditRole(ctx *gin.Context) {
	idStr := ctx.Param("id")
	//  表单数据, 取不到为空串 ""
	title := strings.Trim(ctx.PostForm("title"), " ") // 去除空格
	description := strings.Trim(ctx.PostForm("description"), " ")
	status, err2 := strconv.Atoi(ctx.PostForm("status"))
	id, err := strconv.Atoi(idStr)
	if err != nil || err2 != nil {
		ctx.String(http.StatusOK, "类型转换出错, 数据不合法")
		return
	}
	if title == "" {
		ctx.String(http.StatusOK, "标题不能为空")
		return
	}
	rbacClient := pbRbac.NewRbacRoleService("rbac", MicroClient)
	res, _ := rbacClient.RoleEdit(context.Background(), &pbRbac.RoleEditRequest{
		Id:          int64(id),
		Title:       title,
		Description: description,
		Status:      int64(status),
	})
	ctx.JSON(http.StatusOK, gin.H{
		"success": res.Success,
		"message": res.Message,
	})
}

// 删除
func (ro RoleController) DelRole(ctx *gin.Context) {
	idStr := ctx.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		ctx.String(http.StatusOK, "类型转换出错, 数据参数不合法")
		return
	}
	rbacClient := pbRbac.NewRbacRoleService("rbac", MicroClient)
	res, _ := rbacClient.RoleDelete(context.Background(), &pbRbac.RoleDeleteRequest{
		Id: int64(id),
	})
	ctx.JSON(http.StatusOK, gin.H{
		"success": res.Success,
		"message": res.Message,
	})
}

// 角色授权
func (ro RoleController) Auth(context *gin.Context) {
	// 获取角色ID
	roleId, err1 := strconv.Atoi(context.PostForm("role_id"))
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	// 获取传入的权限ID
	accessIds := context.PostFormArray("access_id_array") // 这里获取到的是切片[]string
	rbacClient := pbRbac.NewRbacRoleService("rbac", MicroClient)
	res, _ := rbacClient.RoleDoAuth(ct.Background(), &pbRbac.RoleDoAuthRequest{
		RoleId:    int64(roleId),
		AccessIds: accessIds,
	})
	context.JSON(http.StatusOK, gin.H{
		"success": res.Success,
		"message": res.Message,
	})
}

// 获取当前角色的全部权限
func (ro RoleController) GetRoleAccess(context *gin.Context) {
	roleId, err := strconv.Atoi(context.Query("role_id"))
	if err != nil {
		context.String(http.StatusBadRequest, "参数不合法")
		return
	}
	/*
		var roleAccessList []RoleAccess
		// 获取角色的所有权限
		DB.Where("role_id = ?", roleId).Find(&roleAccessList)
		// 创建map映射
		roleAccessMap := make(map[int]int)
		for _, v := range roleAccessList {
			roleAccessMap[v.AccessID] = v.AccessID
		}
		// 检索所有顶级模块, 同时预加载所有次级模块
		var accessList []Access
		DB.Where("module_id = ?", 0).Preload("AccessItem").Find(&accessList)
		// 遍历所有权限
		for i := 0; i < len(accessList); i++ {
			// 遍历所有顶级模块
			if _, ok := roleAccessMap[accessList[i].ID]; ok {
				accessList[i].Checked = true
			}
			// 遍历所有次级模块
			for j := 0; j < len(accessList[i].AccessItem); j++ {
				if _, ok := roleAccessMap[accessList[i].AccessItem[j].ID]; ok {
					accessList[i].AccessItem[j].Checked = true
				}
			}
		}
		context.JSON(http.StatusOK, gin.H{
			"accessList": accessList,
		})
	*/
	rbacClient := pbRbac.NewRbacRoleService("rbac", MicroClient)
	res, _ := rbacClient.RoleAuth(ct.Background(), &pbRbac.RoleAuthRequest{
		Id: int64(roleId),
	})
	context.JSON(http.StatusOK, gin.H{
		"accessList": res.AccessList,
	})
}
